package com.gift.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gift.auto.JSONAuthentication;
import com.gift.entity.UserMeans;
import com.gift.mapper.UserMeansMapper;
import com.gift.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author gift
 * @since 2022-03-23
 */
@RestController
@RequestMapping("/user-means")
public class UserMeansController extends JSONAuthentication {
    @Autowired
    UserMeansMapper userMeansMapper;
    @ApiOperation(value = "添加用户的资料")
    @PostMapping("/add")
    public void addCar(HttpServletRequest request, HttpServletResponse response, @RequestBody UserMeans baMeans) throws Exception {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("user_id",baMeans.getUserId());
        UserMeans baMeans1=userMeansMapper.selectOne(wrapper);
        if(baMeans1!=null){
            Result error = Result.error().message("用户资料已存在，不可添加");
            this.WriteJSON(request, response, error);
        }else{
          if(userMeansMapper.insert(baMeans)>0){
              Result ok = Result.ok();
              this.WriteJSON(request, response, ok);
          }else{
              Result error = Result.error();
              this.WriteJSON(request, response, error);
          }
        }
    }
    @ApiOperation(value = "删除用户资料")
    @PostMapping("/del")
    public void delUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id) throws Exception {
        int i = userMeansMapper.deleteById(id);
        if (i > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "根据用户id查询资料")
    @PostMapping("/findById")
    public void findById(HttpServletRequest request, HttpServletResponse response, @RequestParam("userId") int userId) throws Exception {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.like("user_id", userId);
        UserMeans baMeans = userMeansMapper.selectOne(wrapper);
        if (baMeans != null) {
            Result ok = Result.ok().data("mean", baMeans);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查询全部资料")
    @PostMapping("/findByIdAll")
    public void findByIdAll(HttpServletRequest request, HttpServletResponse response,  @RequestParam("size") int size, @RequestParam("current") int current) throws Exception {
        Page<UserMeans> page=new Page<>(current,size);
        Page<UserMeans> page1=userMeansMapper.selectPage(page,null);
        List<UserMeans> list=page1.getRecords();
        int total=userMeansMapper.selectCount(null);
        if (list.size()>0) {
            Result ok = Result.ok().data("mean", list).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error().message("无资料");
            this.WriteJSON(request, response, error);
        }
    }

}

